package com.cgnpc.scp.manager.controller;

import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import com.cgnpc.scp.manager.mybatis.entity.DataDictGroup;
import com.cgnpc.scp.manager.service.IDataDictGroupService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/******************************
 * 用途说明: 数据字典组
 * 作者姓名: yblrs
 * 创建时间: 2020/8/16 17:53
 ******************************/
@Controller
@RequestMapping("/scp/data/dataDictGroup")
public class DataDictGroupController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DataDictGroupController.class);

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private CurrentUser currentUser;

    @Autowired
    private IDataDictGroupService dataDictGroupService;

    /**********************************
     * 用途说明: 数据字典组---分页查询
     * 参数说明: HttpServletRequest request, @RequestBody T reqEntity
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "数据字典组---分页查询")
    @UBA(module = "系统管理", action = "数字字典组分页查询", channel = OperatorType.Page)
    @RequestMapping(value = "/loadByPaging", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends DataDictGroup> Object loadByPaging(HttpServletRequest request, @RequestBody T reqEntity) {
        AjaxResult ajaxResult = null;

        try {
            // UserInfo loginUserInfo = currentUser.getCurrentUserDetail();

            MyPageResult<T> myPageResult = dataDictGroupService.loadByPaging(reqEntity);
            ajaxResult = AjaxResultUtil.createSuccessMessage(myPageResult);
        } catch (Exception e) {
            LOGGER.error("loadByPaging.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

    /**********************************
     * 用途说明: 数据字典组---保存或修改
     * 参数说明: HttpServletRequest request, @RequestBody T reqEntity
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "数据字典组---保存或修改")
    @UBA(module = "系统管理", action = "保存或修改数字字典组", channel = OperatorType.Button)
    @RequestMapping(value = "/saveOrUpdate", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends DataDictGroup> Object saveOrUpdate(HttpServletRequest request, @RequestBody T reqEntity) {
        //{
        //    "dictGroupKey": "test_isValidTest",
        //    "dictGroupName": "是否有效测试",
        //    "dictGroupDesc": "是否有效测试；值Y有效；N无效；默认Y；",
        //    "isValid": "N",
        //    "systemDataDict": "N"
        //}

        AjaxResult ajaxResult = null;

        try {
            // 获取当前的登录用户信息
            UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);

            ajaxResult = dataDictGroupService.saveOrUpdate(loginUserInfo, reqLocale, reqEntity);
        } catch (Exception e) {
            LOGGER.error("saveOrUpdate.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

    /**********************************
     * 用途说明: 数据字典组-查询指定数据字典组信息
     * 参数说明: HttpServletRequest request, @RequestBody T reqEntity
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "数据字典组---查询指定数据字典组信息")
    @UBA(module = "系统管理", action = "查看数字字典组详情", channel = OperatorType.Button)
    @RequestMapping(value = "/queryByDictGroupKey", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends DataDictGroup> Object queryByDictGroupKey(HttpServletRequest request, @RequestBody T reqEntity) {
        //{
        //    "dictGroupKey": "test_isValidTest"
        //}

        AjaxResult ajaxResult = null;

        try {
            // 获取当前的登录用户信息
            // UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            // Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);
            DataDictGroup dbDataDictGroup = dataDictGroupService.queryByDictGroupKey(reqEntity);
            ajaxResult = AjaxResultUtil.createSuccessMessage(dbDataDictGroup);
        } catch (Exception e) {
            LOGGER.error("queryByDictGroupKey.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

}
